package arrays;

/**
 * @author LiangMu
 * @version \$Id: P16.java, v 0.1, 2023年01月03日 13:15 LiangMu Exp $$
 */
public class P16 {
    public static void main(String[] args) {
        int[] ints = new int[]{1, 1, 1, 2, 2, 3};
        System.out.println(removeDuplicates(ints));
    }

    public static int removeDuplicates(int[] nums) {
        int len = nums.length;
        if (len < 3) return len;
        int index = 2;
        for (int i = 2; i < len; ++i) {
            if (nums[i] != nums[index - 2]) {
                nums[index] = nums[i];
                ++index;
            }
        }
        return index;
    }

    public static int removeDuplicates2(int[] nums) {
        int len = 0;
        for(int i = 0; i < nums.length; i++){
            if(len < 2 || nums[i] != nums[len-2]){
                nums[len++] = nums[i];
            }
        }
        return len;
    }
}
